// pages/orderList/orderList.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  timer,
  qrcode = require('../../utils/qrcode.js'),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    isshow: !1,
    issucc:!1,
    showmodal:!1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this

  },
  getdata() {
    wx.showLoading({
      title: '加载中',
    })
    let { page, list } = that.data
    api.getorderrecord(page, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        let { orderArr, nextPage } = res.data.entity.data
        if (orderArr.length > 0) {
          orderArr.forEach(x => {
            list.push(x)
          })
          that.setData({ list, nextPage })
        }
      }
    })
  },
  setshow(e){
    let {qr,id} = e.currentTarget.dataset
    that.setData({ showmodal:!0,qrStr:qr,id},()=>{
      that.getSystemInfo()
    })
  },
  todetail(e) {
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../groupOrderDetail/groupOrderDetail?id=' + id,
    })
  },
  toAfterSales(e) {
    let { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../AfterSales/AfterSales?item=' + JSON.stringify(item),
    })
  },
  confirmReceipt(e) {
    let { id } = e.currentTarget.dataset, orderId = 0
    that.setData({ isshow: !0, orderId: id })
  },
  confirm() {
    let { orderId } = that.data
    api.confirmorder(orderId, res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '确认收货成功',
        })
        that.setData({ isshow: !1, list: [], page: 1 }, () => {
          that.getdata()
        })
      }
    })
  },
  
  close() {
    that.setData({ isshow: !1,issucc:!1 ,showmodal:!1},()=>{
      clearInterval(timer);
    })
  },
  copy(e) {
    let { value } = e.currentTarget.dataset
    wx.setClipboardData({
      data: value,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },
  noAfterSales() {
    wx.showModal({
      title: '提示',
      content: '订单暂未完成，还不能申请售后',
      showCancel: !1,
    })
  },
  nocanConfirmOrder() {
    wx.showModal({
      title: '提示',
      content: '订单暂未发货，还不能确认收货',
      showCancel: !1,
    })
  },
  getSystemInfo() {
    wx.showLoading({
      title: '加载中',
    })
    wx.getSystemInfo({
      success: (res) => {
        new qrcode({
          width: res.screenWidth * 0.515,
          height: res.screenWidth * 0.515,
          canvasId: 'canvas',
          text: that.data.qrStr,
          callback: res => {
            wx.hideLoading();
            that.getisDeal()
          }
        })
      },
    })
  },
  getisDeal(){
    let {showmodal,id} = that.data
    if(showmodal){
      console.log(111);
      timer = setInterval(function () {
        api.ordercheckdeal(id, res => {
          if (res.data.code == 0) {
            if (res.data.entity.data.deal == 1) {
              that.setData({ list: [], page: 1,issucc:!0,showmodal:!1 }, () => {
                that.getdata();
              })
              clearInterval(timer);
            }
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        })
      }, 3000)
      that.setData({ timer })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.setData({ list: [], page: 1 }, () => {
      that.getdata()
    })
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    that.setData({ list: [], page: 1 }, () => {
      that.getdata();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { nextPage, page } = that.data
    if (nextPage == 1) {
      page = page + 1
      that.setData({ page }, () => {
        that.getdata()
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})